<template>
  <div class="app-container">
    <el-row>
      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span><i class="el-icon-cpu"></i> CPU</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <thead>
              <tr>
                <th class="el-table__cell is-leaf">
                  <div class="cell">属性</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">值</div>
                </th>
              </tr>
              </thead>
              <tbody>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">核心数</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.cpu" class="cell">{{ info.cpu.cpuNum }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">用户使用率</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.cpu" class="cell">{{ info.cpu.used }}%</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">系统使用率</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.cpu" class="cell">{{ info.cpu.sys }}%</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">当前空闲率</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.cpu" class="cell">{{ info.cpu.free }}%</div>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>

      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span><i class="el-icon-tickets"></i> 内存</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <thead>
              <tr>
                <th class="el-table__cell is-leaf">
                  <div class="cell">属性</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">内存</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">JVM</div>
                </th>
              </tr>
              </thead>
              <tbody>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">总内存</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.mem" class="cell">{{ info.mem.total }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.jvm" class="cell">{{ info.jvm.total }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">已用内存</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.mem" class="cell">{{ info.mem.used }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.jvm" class="cell">{{ info.jvm.used }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">剩余内存</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.mem" class="cell">{{ info.mem.free }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.jvm" class="cell">{{ info.jvm.free }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">使用率</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.mem" :class="{'text-danger': info.mem.usage > 80}" class="cell">
                    {{ info.mem.usage }}%
                  </div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.jvm" :class="{'text-danger': info.jvm.usage > 80}" class="cell">
                    {{ info.jvm.usage }}%
                  </div>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>

      <el-col :span="24" class="card-box">
        <el-card>
          <div slot="header">
            <span><i class="el-icon-monitor"></i> 服务器信息</span>
          </div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <tbody>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">服务器名称</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.sys" class="cell">{{ info.sys.computerName }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell">操作系统</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.sys" class="cell">{{ info.sys.osName }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">服务器IP</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.sys" class="cell">{{ info.sys.computerIp }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell">系统架构</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.sys" class="cell">{{ info.sys.osArch }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">响应时间</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell">{{ loginTime }}</div>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>

<!--      <el-col :span="24" class="card-box">
        <el-card>
          <div slot="header">
            <span><i class="el-icon-coffee-cup"></i> Java虚拟机信息</span>
          </div>
          <div class="el-table el-table&#45;&#45;enable-row-hover el-table&#45;&#45;medium">
            <table cellspacing="0" style="width: 100%;table-layout:fixed;">
              <tbody>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">Java名称</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.jvm" class="cell">{{ info.jvm.name }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell">Java版本</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.jvm" class="cell">{{ info.jvm.version }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">启动时间</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.jvm" class="cell">{{ info.jvm.startTime }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell">运行时长</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div v-if="info.jvm" class="cell">{{ info.jvm.runTime }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf" colspan="1">
                  <div class="cell">安装路径</div>
                </td>
                <td class="el-table__cell is-leaf" colspan="3">
                  <div v-if="info.jvm" class="cell">{{ info.jvm.home }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf" colspan="1">
                  <div class="cell">项目路径</div>
                </td>
                <td class="el-table__cell is-leaf" colspan="3">
                  <div v-if="info.sys" class="cell">{{ info.sys.userDir }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf" colspan="1">
                  <div class="cell">运行参数</div>
                </td>
                <td class="el-table__cell is-leaf" colspan="3">
                  <div v-if="info.jvm" class="cell">{{ info.jvm.inputArgs }}</div>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>-->

      <el-col :span="24" class="card-box">
        <el-card>
          <div slot="header">
            <span><i class="el-icon-receiving"></i> 磁盘状态</span>
          </div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <thead>
              <tr>
                <th class="el-table__cell el-table__cell is-leaf">
                  <div class="cell">盘符路径</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">文件系统</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">盘符类型</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">总大小</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">可用大小</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">已用大小</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">已用百分比</div>
                </th>
              </tr>
              </thead>
              <tbody v-if="info.sysFiles">
              <tr v-for="(sysFile, index) in info.sysFiles" :key="index">
                <td class="el-table__cell is-leaf">
                  <div class="cell">{{ sysFile.dirName }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell">{{ sysFile.sysTypeName }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell">{{ sysFile.typeName }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell">{{ sysFile.total }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell">{{ sysFile.free }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell">{{ sysFile.used }}</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div :class="{'text-danger': sysFile.usage > 80}" class="cell">{{ sysFile.usage }}</div>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {getServer} from '@/api/monitor/server'

export default {
  name: 'Server',
  data() {
    return {
      // 服务器信息
      // server: [],
      info: {
        cpu: {
          cpuNum: '',
          used: '',
          sys: '',
          free: ''
        },
        py: {
          total: '',
          used: '',
          free: '',
          usage: '',
          name: '',
          version: '',
          startTime: '',
          runTime: '',
          home: ''
        },
        mem: {
          total: '',
          used: '',
          free: '',
          usage: ''
        },
        sys: {
          computerIp: '',
          computerName: '',
          osArch: '',
          osName: '',
          userDir: ''
        },
        sysFiles: [
          {
            dirName: '',
            sysTypeName: '',
            typeName: '',
            total: '',
            used: '',
            free: '',
            usage: ''
          }
        ]
      },
      // 登录时间
      loginTime: new Date(Number(localStorage.getItem("loginTime"))).toLocaleString()
    }
  },
  created() {
    // this.getList();
  },
  mounted() {
    this.getInfo()
  },
  methods: {
    getInfo() {
      let that = this
      that.loading = true
      let url =`${this.$config.VUE_APP_BASE_API}/kms-core-svc/serverInfo`;
      fetch(url, {
        method: 'get',
        headers: {
          'Content-Type': 'application/json',
          'token':localStorage.getItem('token')
        }
      }).then(response => {
        if (!response.ok) {
          throw new Error('网络错误')
        }
        return response.json() // 解析返回的数据为JSON格式
      }).then(data => {
        this.info = data.data
      }).catch(error => {
        console.error('请求失败:', error)
      }).finally(() => {
        that.loading = false
      })
    },
    /** 查询服务器信息 */
    getList() {
      getServer().then(response => {
        this.server = response.data
        // this.$modal.closeLoading();
      })
    }
  }
}
</script>
